package system.schedule;

import java.util.Iterator;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import system.base.Warehouse;

import com.apep.gw.service.StuationAnalyse;
import com.apep.gw.service.impl.ProductStuationAnalyse;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductBidBean;
import com.hitrust.gs.bean.GSProductTemporaryBean;
import com.hitrust.gs.bean.GSProductTemporaryMaintainBean;
import com.hitrust.gs.bean.GSProductTypeBean;
import com.hitrust.gs.bean.GSProductUpdateIsbuyBean;
import com.hitrust.gs.bean.GSProductUpdatePriceBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.dao.GSProductBidDao;
import com.hitrust.gs.dao.GSProductTypeDao;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.vo.GSManufactureVO;

import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.ObjectList;

public class ProductTask implements Job{

	private static boolean isRunning = false;

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		if(!isRunning){
			myRun();
		}else{
			System.out.println("-----------------------------");
		}	
	}

	private void myRun() {
		isRunning = true;
		try {
			// 定时任务执行产品调价
			GSManufactureDAO dao = new GSManufactureDAO();
			GSProductTypeDao typedao = new GSProductTypeDao();
			GSProductBidDao bidDao = new GSProductBidDao();
			GSManufactureProcessor processor = new GSManufactureProcessor();
		    StuationAnalyse productStuationAnalyse =new ProductStuationAnalyse();
			String curDate = DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss");
			List updatePriceList = dao
					.getProductUpdatePricesByDateAndAuditingStatus(curDate, "1");
			Iterator it = updatePriceList.iterator();
			
			while (it.hasNext()) {
				GSProductUpdatePriceBean price = (GSProductUpdatePriceBean) it
						.next();
				GSManufactureBean mbean = processor.getManufInfoByConcentricityId(price.getGs_concentricity_code()); 
				String flag="flase";
		
				
				boolean isSuccess = dao.updateProductByUpdatePrice(
						price.getGs_after_price() + "",
						price.getGs_auditing_user_id(),
						price.getGs_auditing_user(),
						new framework.core.DateUtil().getFullString(), price.getGs_product_id(),
						mbean.getIs_buy(),mbean.getGs_validity());

				if (isSuccess) {
					 GSManufactureVO vo = new GSManufactureVO();
					 vo.setGs_manufacture_unitprice(price.getGs_after_price()+"");
					 vo.setLast_update_time(new framework.core.DateUtil().getFullString());
					 vo.setLast_update_user(price.getGs_auditing_user());
					 vo.setIs_buy("1");
					 vo.setGsValidity("1");
					 productStuationAnalyse.changePrice(processor.appendUpdatePrice(mbean, vo), vo.getLast_update_time(), price.getGs_auditing_user_id(), price.getGs_auditing_user(),mbean!=null?mbean.getId():"");
					 
					 if("0".equals(mbean.getIs_buy())){
						 productStuationAnalyse.open(processor.appendUpdateIsBuy(mbean, vo), vo.getLast_update_time(),price.getGs_auditing_user_id(),price.getGs_auditing_user(),mbean!=null?mbean.getId():"");
						 
							// 设置采购属性为1时 修改对应的产品使用分类和产品招标分类
							GSProductTypeBean bean = typedao
									.findProductTypeByProuductCodeNoStauts(price.getGs_concentricity_code());
							
							GSProductBidBean bidBean = bidDao
							.findProductBidByProuductCodeNoStauts(price.getGs_concentricity_code());
							
							if (bean != null) {
								typedao.updatePrudctTypeIsStatusByProductCode(price.getGs_concentricity_code());
							}else{
								//新增一个产品区域分类
								bean = new GSProductTypeBean();
								bean.setCreateTime(new framework.core.DateUtil().getFullString());
								bean.setCreateUser(price.getGs_auditing_user());
								bean.setCreateUserId(price.getGs_auditing_user_id());
								bean.setIsBalance(1);
								bean.setIsBid(GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID);
								bean.setLastUpdateTime(new framework.core.DateUtil().getFullString());
								bean.setLastUpdateUser(price.getGs_auditing_user());
								bean.setLastUpdateUserId(price.getGs_auditing_user_id());
								bean.setProductCode(price.getGs_concentricity_code());
								bean.setProductType(1);
								bean.setProviderCode(mbean.getGs_provider_id());
								bean.setProductId(mbean.getId());
								bean.setRemark("");
								bean.setStatus(Integer
										.parseInt(ConstDefine.STR_VALID_YES));
								typedao.insert(bean);
								flag="true";
							}
						
							if (bidBean != null) {
								bidDao.updatePrudctIsBidStatusAndTypeAndIsBidByProductCode(price.getGs_concentricity_code());
							}else{
									if("true".equals(flag)){
										bidBean = new GSProductBidBean();
										bidBean.setCreateTime(new framework.core.DateUtil().getFullString());
										bidBean.setCreateUser(price.getGs_auditing_user());
										bidBean.setCreateUserId(price.getGs_auditing_user_id());
										bidBean.setProductName(mbean
												.getGs_manufacture_name());
										bidBean.setLastUpdateTime(new framework.core.DateUtil().getFullString());
										bidBean.setLastUpdateUser(price.getGs_auditing_user());
										bidBean.setLastUpdateUserId(price.getGs_auditing_user_id());
										bidBean.setProductCode(price.getGs_concentricity_code());
										bidBean.setProductType(GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID);
										bidBean.setProviderCode(mbean
												.getGs_provider_id().trim());
										bidBean.setProductId(mbean.getId());
										bidBean.setRemark("");
										bidBean.setStatus(Integer
												.parseInt(ConstDefine.STR_VALID_YES));
										bidDao.insert(bidBean);
									}
							}
					 }
					 dao.updateProductUpdatePriceStatusSql("1",price.getGs_id());
				} else {
					 dao.updateProductUpdatePriceStatusSql("2",price.getGs_id());
				}
			}
			// 定时任务执行产品启用或关闭
			List updateIsbuyList = dao
					.getProductUpdateIsbuysByDateAndAuditingStatus(curDate, "1");
			Iterator updateIsBuyIt = updateIsbuyList.iterator();
			while (updateIsBuyIt.hasNext()) {
				GSProductUpdateIsbuyBean isbuy = (GSProductUpdateIsbuyBean) updateIsBuyIt
						.next();
				GSManufactureBean mbean = processor.getManufInfoByConcentricityId(isbuy.getGs_concentricity_code());
				boolean isSuccess = dao.updateProductByUpdateIsbuy(
						isbuy.getGs_type(), isbuy.getGs_auditing_user_id(),
						isbuy.getGs_auditing_user(),
						new framework.core.DateUtil().getFullString(), isbuy.getGs_product_id());
				if (isSuccess) {
					if("0".equals(isbuy.getGs_type())){
						 GSManufactureVO vo = new GSManufactureVO();
						 vo.setIs_buy(isbuy.getGs_type());
						 vo.setLast_update_time(new framework.core.DateUtil().getFullString());
						 vo.setLast_update_user(isbuy.getGs_auditing_user());
						 productStuationAnalyse.close(processor.appendUpdateIsBuy(mbean, vo), vo.getLast_update_time(),isbuy.getGs_auditing_user_id(),isbuy.getGs_auditing_user(),mbean!=null?mbean.getId():"");
						 
						// 设置采购属性为0时 修改对应的产品使用分类和产品招标分类
							GSProductTypeBean bean = typedao
									.findProductTypeByProuductCodeNoStauts(isbuy.getGs_concentricity_code());
							if (bean != null) {
								typedao.updatePrudctTypeStatusByProductCode(isbuy.getGs_concentricity_code());
							}
							GSProductBidBean bidBean = bidDao
									.findProductBidByProuductCodeNoStauts(isbuy.getGs_concentricity_code());
							if (bidBean != null) {
								bidDao.updatePrudctBidStatusByProductCode(isbuy.getGs_concentricity_code());
							}
					
					}else{
						 GSManufactureVO vo = new GSManufactureVO();
						 vo.setIs_buy(isbuy.getGs_type());
						 vo.setLast_update_time(new framework.core.DateUtil().getFullString());
						 vo.setLast_update_user(isbuy.getGs_auditing_user());
						 productStuationAnalyse.open(processor.appendUpdateIsBuy(mbean, vo), vo.getLast_update_time(),isbuy.getGs_auditing_user_id(),isbuy.getGs_auditing_user(),mbean!=null?mbean.getId():"");
						 
						// 设置采购属性为1时 修改对应的产品使用分类和产品招标分类
							GSProductTypeBean bean = typedao
									.findProductTypeByProuductCodeNoStauts(isbuy.getGs_concentricity_code());
							
							GSProductBidBean bidBean = bidDao
							.findProductBidByProuductCodeNoStauts(isbuy.getGs_concentricity_code());
							
							
							String productTypeStr=isbuy.getGs_product_type()+"";
							String productBidStr=isbuy.getGs_product_bid()+"";
							
							if (bean != null) {
								String productType=bean.getProductType()+"";
								if(productTypeStr.equals(productType)){
									if("1".equals(isbuy.getGs_product_type())){	
										typedao.updatePrudctTypeAndStatusByProductCode(
												isbuy.getGs_product_type(),isbuy.getGs_concentricity_code(),isbuy.getGs_product_bid());
									}else{
										typedao.updatePrudctTypeAndStatusByProductCode(
												isbuy.getGs_product_type(),isbuy.getGs_concentricity_code(),"99");
									}
								}else{
									String idbidStr="";
									if("1".equals(isbuy.getGs_product_type())){
										idbidStr=isbuy.getGs_product_bid();
									}else{
										idbidStr="99";
									}
									
									//修改原来的区域分类并将status改为1
									typedao.updatePrudctTypeAndStatusByProductCode(
											isbuy.getGs_product_type(),isbuy.getGs_concentricity_code(),idbidStr);
								}
								
							}else{
								//新增一个产品区域分类
								bean = new GSProductTypeBean();
								bean.setCreateTime(new framework.core.DateUtil().getFullString());
								bean.setCreateUser(isbuy.getGs_auditing_user());
								bean.setCreateUserId(isbuy.getGs_auditing_user_id());
								bean.setIsBalance(1);
								String productBid="";
								
								if("1".equals(isbuy.getGs_product_type())){
									productBid=isbuy.getGs_product_bid();
								}
								
								bean.setIsBid(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
										: Integer.parseInt(productBid));
								bean.setLastUpdateTime(new framework.core.DateUtil().getFullString());
								bean.setLastUpdateUser(isbuy.getGs_auditing_user());
								bean.setLastUpdateUserId(isbuy.getGs_auditing_user_id());
								bean.setProductCode(isbuy.getGs_concentricity_code());
								bean.setProductType(Integer.parseInt(isbuy.getGs_product_type()));
								bean.setProviderCode(mbean.getGs_provider_id());
								bean.setProductId(mbean.getId());
								bean.setRemark("");
								bean.setStatus(Integer
										.parseInt(ConstDefine.STR_VALID_YES));
								typedao.insert(bean);
								
							}
						
							if (bidBean != null) {
								String productBid=bidBean.getProductType()+"";
								if(productBidStr.equals(productBid)){
									if("1".equals(isbuy.getGs_product_type())){//1类区域的情况,修改原招标分类,并将status改为1
										bidDao.updatePrudctBidAndStatus(isbuy.getGs_product_bid(),isbuy.getGs_concentricity_code());
									}
								}else{
									if("1".equals(isbuy.getGs_product_type())){//1类区域的情况,修改原招标分类,并将status改为1
										bidDao.updatePrudctBidAndStatus(isbuy.getGs_product_bid(),isbuy.getGs_concentricity_code());
									}else{//不是1类区域,将原招标分类的status改为0
										bidDao.updatePrudctBidStatusByProductCode(isbuy.getGs_concentricity_code());
									}
								}
							
							}else{
									if("1".equals(isbuy.getGs_product_type())){//1类区域,新增一条招标分类的数据。
										bidBean = new GSProductBidBean();
										bidBean.setCreateTime(new framework.core.DateUtil().getFullString());
										bidBean.setCreateUser(isbuy.getGs_auditing_user());
										bidBean.setCreateUserId(isbuy.getGs_auditing_user_id());
										bidBean.setProductName(mbean
												.getGs_manufacture_name());
										bidBean.setLastUpdateTime(new framework.core.DateUtil().getFullString());
										bidBean.setLastUpdateUser(isbuy.getGs_auditing_user());
										bidBean.setLastUpdateUserId(isbuy.getGs_auditing_user_id());
										bidBean.setProductCode(isbuy.getGs_concentricity_code());
										
										String productBid="";
										if("1".equals(isbuy.getGs_product_type())){
											productBid=isbuy.getGs_product_bid();
										}
										
										bidBean.setProductType(productBid.equals("") ? GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID
												: Integer.parseInt(productBid));
										bidBean.setProviderCode(mbean
												.getGs_provider_id().trim());
										bidBean.setProductId(mbean.getId());
										bidBean.setRemark("");
										bidBean.setStatus(Integer
												.parseInt(ConstDefine.STR_VALID_YES));
										bidDao.insert(bidBean);
									}
							}
							
							
					}
					dao.updateProductUpdateIsbuyStatusSql("1",isbuy.getGs_id());
				} else {
					dao.updateProductUpdateIsbuyStatusSql("2",isbuy.getGs_id());
				}
			}

			// 定时任务执行产品替换
			List temporaryMaintainList = dao
					.getProductTemporaryMaintainsByDateAndAuditingStatus(
							curDate, "1");
			Iterator temporaryMaintainIt = temporaryMaintainList.iterator();
			while (temporaryMaintainIt.hasNext()) {
				GSProductTemporaryMaintainBean temp = (GSProductTemporaryMaintainBean) temporaryMaintainIt
						.next();
				GSManufactureBean mbean = dao
						.getManufactureByConcentricityCode(
								temp.getGs_temporary_concentricity_code(),
								" select * from t_product ");
				ObjectList warehouseList = Warehouse.fetchAllWarehouseCode(
						DBConnection.open(false), mbean.getWarehouse_code());
				for (int i = 0; i < warehouseList.size(); i++) {
					Warehouse warehouse = (Warehouse) warehouseList.get(i);
					GSProductTemporaryBean bean = new GSProductTemporaryBean();
					bean.setGs_concentricity_code(temp
							.getGs_concentricity_code());
					bean.setGs_temporary_concentricity_code(temp
							.getGs_temporary_concentricity_code());
					bean.setWarehouse_code(warehouse.getCode());
					bean.setWarehouse_name(warehouse.getName());
					bean.setGs_create_user_id(temp.getGs_create_user_id());
					bean.setGs_create_user(temp.getGs_create_user());
					bean.setGs_create_time(new framework.core.DateUtil().getFullString());
					bean.setGs_last_update_user_id(temp
							.getGs_auditing_user_id());
					bean.setGs_last_update_user(temp.getGs_auditing_user());
					bean.setGs_last_update_time(new framework.core.DateUtil().getFullString());
					boolean isSuccess =dao.insertProductTemporary(bean);
					if (isSuccess) {
						dao.updateProductTemporaryMaintainStatusSql("1", temp.getGs_id());
					} else {
						dao.updateProductTemporaryMaintainStatusSql("2", temp.getGs_id());
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		isRunning = false;
	}
}
